<template>
  <div class="center">
    <div class="search clearfix">
      <el-input
        v-model="accounts"
        :clearable="true"
        placeholder="请输入员工账号"
        :style="{ width: '200px' }"
      ></el-input>
      <el-input
        v-model="username"
        :clearable="true"
        placeholder="请输入员工姓名"
        :style="{ width: '200px' }"
      ></el-input>
      <el-button type="primary" @click="getstaff">查询</el-button>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="table">
      <dv-border-box-11 :color="['#11caff']" backgroundColor="transparent" title="员工信息">
      <el-table
        :data="tableData"
        class="customer-table"
        height="71.5vh"
        :header-cell-style="headClass"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="accounts" label="员工账号"> </el-table-column>
        <el-table-column prop="userName" label="员工姓名" align="center">
        </el-table-column>
        <el-table-column label="员工类型" align="center">
          <template slot-scope="scope">
            <i v-if="scope.row.type=='admin'">超级管理员</i>
            <i v-if="scope.row.type=='leader'">区域主管</i>
            <i v-if="scope.row.type=='common'">普通员工</i>
          </template>
        </el-table-column>
        <el-table-column prop="mail" label="邮箱" align="center">
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" align="center">
        </el-table-column>
        <el-table-column prop="warning" label="员工信息" align="center">
          <template slot-scope="scope">
            <span @click="handle(scope.row)">员工详情</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="change(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        @prev-click="prevclick"
        @next-click="nextclick"
        :current-page="currentPage"
        :page-size="pagesize"
        layout="total,prev, pager, next"
        :total="total"
      >
      </el-pagination>
      </dv-border-box-11>
    </div>

    <!-- 员工详情 -->
    <el-dialog
      class="staff-info"
      title="员工详情"
      :inline="true"
      :visible.sync="dialogFormVisible1"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="staffform"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="用户账号">
          <el-input :value="staffform.accounts" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input :value="staffform.mail" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input :value="staffform.mobile" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="员工类型">
          <el-input :value="staffform.type" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="员工id">
          <el-input :value="staffform.userId" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="员工名称">
          <el-input :value="staffform.userName" readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 员工编辑 -->
    <el-dialog
      class="staff-update"
      title="员工编辑"
      :inline="true"
      :visible.sync="dialogFormVisible2"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="updateform"
        label-position="left"
        label-width="100px"
        ref="updateruleform"
        :rules="updaterules"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="updateform.userName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="updateform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="updateform.mail"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatestaff(updateform)"
            >修改</el-button
          >
          <el-button @click="updateout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增员工 -->
    <el-dialog
      class="staff-add"
      title="新增员工"
      :inline="true"
      :visible.sync="dialogFormVisible3"
      :append-to-body="true"
    >
      <el-form
        class="clearfix"
        :model="addform"
        label-position="left"
        label-width="100px"
        ref="addruleform"
        :rules="addrules"
      >
        <el-form-item label="员工账号" prop="accounts">
          <el-input v-model="addform.accounts"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="userName">
          <el-input v-model="addform.userName"></el-input>
        </el-form-item>
        <el-form-item label="租户账号" prop="Account">
          <el-select v-model="addform.Account">
            <el-option
              v-for="item in accountlist"
              :label="item.companyName"
              :key="item.index"
              :value="item.accounts"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工类型" prop="stafftype">
          <el-select v-model="addform.stafftype">
            <el-option
              v-for="(item, key) in typelist"
              :label="item"
              :key="item"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="type">
          <el-select v-model="addform.type">
            <el-option
              v-for="item in rolelist"
              :label="item.name"
              :key="item.index"
              :value="item.roleId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addform.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="addform.mail"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="addform.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input type="password" v-model="addform.repassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addstaff(addform)">新增</el-button>
          <el-button @click="addout">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "staffinfo",
  data() {
    //员工手机验证
    let validMobile = (rule, value, callback) => {
      if (value == "" || value == undefined) {
        callback(new Error("手机号不能为空"));
      } else {
        let reg = /^1[3|4|5|7|8][0-9]{9}$/;
        if (!reg.test(value)) {
          callback(new Error("手机号格式不对"));
        } else {
          callback();
        }
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.addform.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      accounts: "",
      username: "",
      currentPage: 1,
      pagesize: 10,
      total: 0,
      tableData: [],
      dialogFormVisible1: false, //员工详情弹窗
      dialogFormVisible2: false, //员工编辑弹窗
      dialogFormVisible3: false, //新增员工弹窗
      staffform: {},
      addform: {
        accounts: "",
        mail: "",
        mobile: "",
        password: "",
        repassword: "",
        roleName: "",
        Account: "",
        stafftype: "",
        type: "",
        userName: "",
      },
      addrules: {
        Account: [{ required: true, message: "租户不能为空", trigger: "blur" }],
        accounts: [
          { required: true, message: "账号不能为空", trigger: "blur" },
        ],
        mobile: [{ required: true, validator: validMobile, trigger: "blur" }],
        mail: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { type: "email", message: "邮箱格式不正确", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择角色类型", trigger: "change" },
        ],
        stafftype: [
          { required: true, message: "请选择员工类型", trigger: "change" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        repassword: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        userName: [
          { required: true, message: "员工名称不能为空", trigger: "blur" },
        ],
      },
      addruleform: {
        mobile: "",
        mail: "",
        type: "",
        Account: "",
        accounts: "",
        repassword: "",
        password: "",
        userName: "",
        stafftype: "",
      },
      updaterules: {
        mobile: [{ required: true, validator: validMobile, trigger: "blur" }],
        mail: [{ type: "email", message: "邮箱格式不正确", trigger: "blur" }],
      },
      updateruleform: {
        mobile: "",
      },
      updateform: {},
      accountlist: [],
      typelist: [],
      rolelist: [],
    };
  },
  watch:{
    currentPage(){
      this.getstaff();
    }
  },
  methods: {
    headClass() {
      return "background-color: transparent;color:#fff;font-weight:bold;";
    },
    //获取员工列表
    getstaff() {
      let params = {
        userName: this.username || "",
        page: {
          pageNo: this.currentPage,
          pageSize: this.pagesize,
        },
      };
      this.$http
        .post("/user/list", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.total = res.data.b.totalCount;
            this.tableData = res.data.b.result;
          }
        })
        .catch((err) => {});
    },
    //获取角色下拉列表
    getrolelist() {
      this.$http
        .post("/role/select", {})
        .then((res) => {
          if (res.data.h.code == 0) {
            this.rolelist = res.data.b;
          }
        })
        .catch((err) => {});
    },
    //获取角色下拉列表
    gettypelist() {
      this.$http
        .get("/user/userType", {})
        .then((res) => {
          if (res.data.h.code == 0) {
            this.typelist = res.data.b;
          }
        })
        .catch((err) => {});
    },
    //获取租户下拉列表
    getaccountlist() {
      this.$http
        .get("/tenant/tenantList")
        .then((res) => {
          if (res.data.h.code == 0) {
            this.accountlist = res.data.b;
          }
        })
        .catch((err) => {});
    },
    //新增
    add() {
      this.dialogFormVisible3 = true;
      this.getrolelist();
      this.getaccountlist();
      this.gettypelist();
    },

    //新增员工表单清空
    addformclear() {
      for (let i in this.addform) {
        this.addform[i] = "";
      }
    },
    //新增员工结束
    addout() {
      this.$refs.addruleform.clearValidate();
      this.dialogFormVisible3 = false;
      this.addformclear();
    },
    //编辑员工结束
    updateout() {
      this.$refs.updateruleform.clearValidate();
      this.dialogFormVisible2 = false;
      this.updateformclear();
    },
    //编辑员工表单清空
    updateformclear() {
      for (let i in this.updateform) {
        this.updateform[i] = "";
      }
    },
    addstaff(form) {
      let params = {
        accounts: form.accounts,
        tenantAccounts: form.Account,
        departmentId: form.departmentId,
        departmentName: form.departmentName,
        mail: form.mail,
        mobile: form.mobile,
        password: form.password,
        rePassword: form.repassword,
        roleId: form.type,
        roleName: "yixun",
        type: form.stafftype,
        userName: form.userName,
      };
      this.$refs.addruleform.validate((valid) => {
        if (valid) {
          this.$http
            .post("/user/add", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.addout();
                this.getstaff();
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    handleChange(val) {
      this.type = val;
    },
    //上一页
    prevclick() {
      this.currentPage--;
    },
    //下一页
    nextclick() {
      this.currentPage++;
    },
    //分页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    //员工详情
    handle(row) {
      let params = {
        userId: row.userId,
      };
      this.$http
        .post("/user/info", params)
        .then((res) => {
          if (res.data.h.code == 0) {
            this.dialogFormVisible1 = true;
            this.staffform = res.data.b;
          }
        })
        .catch((err) => {});
    },
    //员工编辑开始
    change(row) {
      this.dialogFormVisible2 = true;
      this.updateform = Object.assign(this.updateform, row);
    }, 
    updatestaff(form) {
      let params = {
        mobile: form.mobile,
        userId: form.userId,
        userName: form.userName,
      };
      this.$refs.updateruleform.validate((valid) => {
        if (valid) {
          this.$http
            .post("/user/update", params)
            .then((res) => {
              if (res.data.h.code == 0) {
                this.updateout();
                this.getstaff();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              }
            })
            .catch((err) => {});
        } else {
          this.$message({
            message: "表单填写有误，请重新填写",
            type: "warning",
          });
          return false;
        }
      });
    },
    //员工删除
    handleDelete(row) {
      this.$confirm("此操作将删除该员工, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            userId: row.userId,
          };
          this.$http.post("/user/del", params).then((res) => {
            if (res.data.h.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //重新请求员工列表
              this.getstaff();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getstaff();
  },
};
</script>
<style scoped>
.center {
  position: relative;
}
.search {
  padding-left: 20px;
  box-sizing: border-box;
  display: inline-block;
}
.search .el-select {
  float: left;
  width: 120px;
  margin-right: 10px;
}
.search .el-input {
  float: left;
  width: 150px;
  margin-right: 10px;
}
.table {
  box-sizing: border-box;
  height:83vh;
}
.table .cell > span {
  cursor: pointer;
  color: rgba(0, 255, 255, 1);
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.staff-info .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.staff-add .el-form {
  padding: 15px;
  box-sizing: border-box;
}
.staff-add .el-form .el-form-item {
  display: inline-block;
  width: 45%;
}
.staff-add .el-form .el-form-item:nth-of-type(2n + 1) {
  margin-right: 2%;
}
.staff-add .el-form .el-form-item:nth-last-of-type(1) {
  width: 100%;
}
.staff-add .el-form .el-form-item .el-select {
  display: block;
}

.staff-info .el-form .el-form-item {
  display: inline-block;
  width: 45%;
}
.staff-info .el-form .el-form-item {
  display: inline-block;
  width: 45%;
}
.staff-info .el-form .el-form-item:nth-of-type(2n + 1) {
  margin-right: 2%;
}
.staff-update .el-form .el-form-item .el-select {
  display: block;
}
</style>
<style>
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
</style>